package com.example.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.base.entity.Skill;
import com.example.base.service.SkillService;
import com.example.base.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/skill")
public class SkillController {

    @Autowired
    SkillService skillService;

    @GetMapping("/get")
    public Response getSkill(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size",required = false) Integer size,
            Skill skill
    ){
        if (ObjectUtils.isEmpty(page)) {
            page = 1;
        }
        if (ObjectUtils.isEmpty(size)) {
            size = 5;
        }

        IPage<Skill> skillIPage = skillService.selectSkill(skill, page, size);
        skillIPage.setTotal(skillIPage.getTotal());

        Response response = new Response();
        response.setData(skillIPage);
        response.setMsg("标签查询成功");
        response.setStatus(200);

        return response;
    }

    @GetMapping("/getAll")
    public Response getAllSkill(){

        List<Skill> list = skillService.selectAllSkill();

        Response response = new Response();
        response.setData(list);
        response.setMsg("所有标签查询成功");
        response.setStatus(200);

        return response;
    }

    @PostMapping("/add")
    public Response addSkill(@RequestBody Skill skill){

        // 生成添加时间
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = simpleDateFormat.format(now);

        skill.setCreateTime(createTime);

        Response response = new Response();

        int statusNum = skillService.addSkill(skill);

        if (statusNum == 1) {
            response.setStatus(200);
            response.setData(statusNum);
            response.setMsg("标签类型成功");
        }
        return response;
    }

    @PutMapping("/update")
    public Response updateSkill(@RequestBody Skill skill){

        // 生成添加修改时间
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String updateTime = simpleDateFormat.format(now);

        skill.setUpdateTime(updateTime);

        Response response = new Response();

        int statusNum = skillService.updateSkill(skill);

        if (statusNum == 1){
            response.setStatus(200);
            response.setData(statusNum);
            response.setMsg("修改标签成功");
        }

        return response;
    }

    @PutMapping("/del")
    public Response delSkill(
            @RequestParam(value = "skillId", required = true) String skillId
    ){

        Response response = new Response();

        int statusNum = skillService.delSkill(skillId);

        response.setData(statusNum);
        if (statusNum == 1){
            response.setStatus(200);
            response.setMsg("执行删除命令成功");
        }else {
            response.setStatus(400);
            response.setMsg("执行删除命令成功，但没有此skillId");
        }

        return response;
    }
}
